#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: relation.py 
@version:
@time: 2022/01/06 
"""
from TapException.TM import DataFlowRelationBuildError


class TaskMapTable:
    """
    迁移任务 sink 配置
    """

    def __init__(self, kwargs):
        self.syncObjects = kwargs.get('syncObjects', {})
        if type(self.syncObjects) is not dict:
            raise DataFlowRelationBuildError
        self.tablePrefix = kwargs.get('tablePrefix', '')
        self.tableSuffix = kwargs.get('tableSuffix', '')
        self.tableNameTransform = kwargs.get('tableNameTransform', '')
        self.fieldsNameTransform = kwargs.get('fieldsNameTransform', '')
        self.batchOperationList = kwargs.get('batchOperationList', [])


class Association:
    def __init__(self, kwargs):
        """
        source： name type tableName
        sink: name type tableName updateConditionFields
        :param kwargs:
        """
        self.source = kwargs.get('source')
        self.sink = kwargs.get('sink')


# merge as one , 文档最外层，可以是customer也可以是policy
class MergeAsOne:
    def __init__(self, kwargs):
        self.merge = {
            'children': [],
            'id': '',
            'isArray': False,
            'mergeType': 'updateOrInsert',
            'tableName': kwargs.get('source').get('tableName')
        }
        self.source = kwargs.get('source')


class MergeAsList:
    def __init__(self, kwargs):
        self.merge = {
            'children': [],
            'arrayKeys': ['pk'],
            'id': '',
            'isArray': False,
            'joinKeys': [{'source': 'pk',
                          'target': 'pk'}],
            'mergeType': 'updateIntoArray',
            'tableName': kwargs.get('source').get('tableName'),
            'targetPath': 'AsMoreList'
        }
        self.source = kwargs.get('source')


class MergeAsObject:
    def __init__(self, kwargs):
        self.merge = {
            'children': [],
            'id': '',
            'isArray': False,
            'joinKeys': [{'source': 'pk',
                          'target': 'pk'}],
            'mergeType': 'updateWrite',
            'tableName': kwargs.get('source').get('tableName'),
            'targetPath': 'AsOneObject'
        }
        self.source = kwargs.get('source')


class MergeListAsObject:
    def __init__(self, kwargs):
        self.merge = {
            'children': [],
            'id': '',
            'isArray': False,
            'joinKeys': [{'source': 'pk',
                          'target': 'AsMoreList.pk'}],
            'mergeType': 'updateWrite',
            'tableName': kwargs.get('source').get('tableName'),
            'targetPath': 'AsMoreList.AsOneObject'
        }
        self.source = kwargs.get('source')


# Merge Association sink not update, set to None
class MergeAssociation:
    def __init__(self, kwargs):
        """
        source： name type tableName
        sink: name type tableName updateConditionFields
        :param kwargs:
        """
        self.source = kwargs.get('source')
        self.sink = kwargs.get('sink')
